﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;

public static class EasySDKManager
{
    private static SDKImpBase imp = null;
    public static void Init(EasySDKCallBackListener listener)
    {
        if(Application.platform == RuntimePlatform.Android)
        {
            imp = new AndroidEasySDKImp();
        }else if(Application.platform == RuntimePlatform.IPhonePlayer)
        {
            imp = new IOSEasySDKImp();
        }

        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Init);
        data.Add(EasySDKConstParameterName.ParameterName_ListenerName, listener.gameObject.name);
        CallFunction(data);
    }

    public static void Login()
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Login);

        CallFunction(data);
    }
    public static void Logout()
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Logout);

        CallFunction(data);
    }

    public static void Pay(OrderInfo  orderInfo)
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Pay);
        Type type = orderInfo.GetType();
        FieldInfo[] fields= type.GetFields();
        for (int i = 0; i < fields.Length; i++)
        {
            FieldInfo f = fields[i];
            object obj = f.GetValue(orderInfo);
            string value =obj==null?"": obj.ToString();
            data.Add(f.Name, value);
        }
        CallFunction(data);
    }

    public static void QuitGame()
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Other);
        data.Add(EasySDKConstParameterName.ParameterName_OtherFunctionName, EasySDKConstFunctionName.FunctionName_QuitGame);
        CallFunction(data);
    }

    public static void DataReport_Login(string accountId)
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Log);
        data.Add(EasySDKConstParameterName.ParameterName_LogFunctionName, EasySDKConstFunctionName.FunctionName_Login);
        data.Add(EasySDKConstParameterName.ParameterName_AccountId, accountId);
        CallFunction(data);
    }
    public static void DataReport_Logout()
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Log);
        data.Add(EasySDKConstParameterName.ParameterName_LogFunctionName, EasySDKConstFunctionName.FunctionName_Logout);
        CallFunction(data);
    }
    public static void DataReport_Event(string eventID,string eventLabel,string eventMap)
    {
        Dictionary<string, string> data = new Dictionary<string, string>();
        data.Add(EasySDKConstParameterName.ParameterName_FunctionName, EasySDKConstFunctionName.FunctionName_Log);
        data.Add(EasySDKConstParameterName.ParameterName_LogFunctionName, EasySDKConstFunctionName.FunctionName_Login);
        data.Add(EasySDKConstParameterName.ParameterName_EventID, eventID);
        data.Add(EasySDKConstParameterName.ParameterName_EventLabel, eventLabel);
        data.Add(EasySDKConstParameterName.ParameterName_EventMap, eventMap);
        CallFunction(data);
    }

    private static void CallFunction(Dictionary<string, string> data)
    {
        string str = Serializer.Serialize(data);
        Debug.Log("CallFunction : " + str);
        imp.CallFunction(str);
    }
}

